package com.hqyj.controller;

import com.hqyj.pojo.Employee;
import com.hqyj.pojo.PageData;
import com.hqyj.pojo.Student;
import com.hqyj.service.StudentService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping({"/student"})
public class StudentController {

    @Autowired
    private StudentService studentService;

    //学生信息主页面跳转
    /*@RequestMapping({"/list"})
    public String getAll(ModelMap modelMap) {
        List<Student> all = this.studentService.getAll();
        modelMap.put("studentList", all);
        return "student/studentList";
    }*/

    //删除操作
    @RequestMapping({"/delete"})
    /*public String deleteOne(ModelMap modelMap, Integer id) {
        studentService.deleteById(id);
        List<Student> all = this.studentService.getAll();
        modelMap.put("studentList", all);
        return "student/studentList";
    }*/

    public String deleteOne(Integer pageNumber, Integer pageSize, ModelMap modelMap, Integer id) {
        studentService.deleteById(id);
        int number;
        int size;
        if(pageNumber == null){
            number = 1; //默认显示第1页
        }else{
            number = pageNumber;
        }
        if(pageSize == null){
            size = 5; //默认每页显示两条
        }else{
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Student> pageData = studentService.getUserByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("studentList", pageData);
        //请求转发到success.jsp
        return "student/studentList";
    }

    /*//添加学生信息页面跳转
    @RequestMapping({"/studentAdd"})
    public String studentAddOne() {
        return "student/studentAdd";
    }*/

    //增加学生信息
    @RequestMapping({"/add"})
    @ResponseBody
    public Student addOne(@RequestBody Student student) {
        System.out.println(student);
        studentService.insertOne(student);
        return student;
    }

   /* //修改学生信息页面
    @RequestMapping({"/studentEdit"})
    public String studentEditOne(ModelMap modelMap,Integer id) {
        Student student=studentService.selectById(id);
        modelMap.put("studentList", student);
        return "student/studentEdit";
    }*/

    //修改学生信息
    @RequestMapping({"/edit"})
    @ResponseBody
    public Student editOne(@RequestBody Student student) {
        System.out.println(student);
        studentService.updateOne(student);
        return student;
    }


    //分页显示
    @RequestMapping("/list")
    public String getAllUserByPage(Integer pageNumber, Integer pageSize, ModelMap modelMap){
        //处理参数
        int number;
        int size;
        if(pageNumber == null){
            number = 1; //默认显示第1页
        }else{
            number = pageNumber;
        }
        if(pageSize == null){
            size = 5; //默认每页显示两条
        }else{
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Student> pageData = studentService.getUserByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("studentList", pageData);
        //请求转发到success.jsp
        return "student/studentList";
    }
}



